package com.papelera;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import com.fichaClinica.R;
import com.fichaClinica.R.id;
import com.fichaClinica.R.layout;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AlertDialogWebImage extends Dialog {

  

	private String image_URL="http://radiografia2006.com.ar/imagenes/385018.jpg"; 
    private String name;
   // EditText etName;

    public AlertDialogWebImage(Context context, String name, ReadyListener readyListener) {
        super(context);
        this.name = name;
      //  this.readyListener = readyListener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webimage);
        
    	ImageView bmImage = (ImageView) findViewById(R.id.image);
		BitmapFactory.Options bmOptions;
		bmOptions = new BitmapFactory.Options();
		bmOptions.inSampleSize = 1;
		Bitmap bm = LoadImage(image_URL, bmOptions);
		bmImage.setImageBitmap(bm);
        
        
        
      setTitle("Radiografia ");
      Button buttonCerrar = (Button) findViewById(R.id.btnCancelar);
      buttonCerrar.setOnClickListener(new OKListener());
      
    }

    private class OKListener implements android.view.View.OnClickListener {
        public void onClick(View v) {
      
        	AlertDialogWebImage.this.dismiss();
		}
	}

	public interface ReadyListener {
		public void ready(String name);
	}


	
	
	
	
	
	

	/** Called when the activity is first created. */
	/*
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webimage);

		ImageView bmImage = (ImageView) findViewById(R.id.image);
		BitmapFactory.Options bmOptions;
		bmOptions = new BitmapFactory.Options();
		bmOptions.inSampleSize = 1;
		Bitmap bm = LoadImage(image_URL, bmOptions);
		bmImage.setImageBitmap(bm);
	}
	
	*/
	

	private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
		Bitmap bitmap = null;
		InputStream in = null;
		try {
			in = OpenHttpConnection(URL);
			bitmap = BitmapFactory.decodeStream(in, null, options);
			in.close();
		} catch (IOException e1) {
		}
		return bitmap;
	}

	private InputStream OpenHttpConnection(String strURL) throws IOException {
		InputStream inputStream = null;
		URL url = new URL(strURL);
		URLConnection conn = url.openConnection();

		try {
			HttpURLConnection httpConn = (HttpURLConnection) conn;
			httpConn.setRequestMethod("GET");
			httpConn.connect();

			if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				inputStream = httpConn.getInputStream();
			}
		} catch (Exception ex) {
		}
		return inputStream;
	}

	
	
	public String getImage_URL() {
		return image_URL;
	}

	public void setImage_URL(String image_URL) {
		this.image_URL = image_URL;
	}
}



/*

	*/
	
	/* public class ImageLoadingTask extends AsyncTask<String, Void, Bitmap> {
		
			    private String url;
			    private final WeakReference<ImageView> imageViewReference;

			    public BitmapDownloaderTask(ImageView imageView) {
			        imageViewReference = new WeakReference<ImageView>(imageView);
			    }

			    @Override
			    // Actual download method, run in the task thread
			    protected Bitmap doInBackground(String... params) {
			         // params comes from the execute() call: params[0] is the url.
			         return downloadBitmap(params[0]);
			    }

			    @Override
			    // Once the image is downloaded, associates it to the imageView
			    protected void onPostExecute(Bitmap bitmap) {
			        if (isCancelled()) {
			            bitmap = null;
			        }

			        if (imageViewReference != null) {
			            ImageView imageView = imageViewReference.get();
			            if (imageView != null) {
			                imageView.setImageBitmap(bitmap);
			            }
			        }
			    }
			}

	static class DownloadedDrawable extends ColorDrawable {
		private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;

		public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
			super(Color.BLACK);
			bitmapDownloaderTaskReference = new WeakReference<BitmapDownloaderTask>(
					bitmapDownloaderTask);
		}

		public BitmapDownloaderTask getBitmapDownloaderTask() {
			return bitmapDownloaderTaskReference.get();
		}
	}

	public void download(String url, ImageView imageView) {
		if (cancelPotentialDownload(url, imageView)) {
			BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
			DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
			imageView.setImageDrawable(downloadedDrawable);
			task.execute(url, cookie);
		}
	}

	private static boolean cancelPotentialDownload(String url,
			ImageView imageView) {
		BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);

		if (bitmapDownloaderTask != null) {
			String bitmapUrl = bitmapDownloaderTask.url;
			if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
				bitmapDownloaderTask.cancel(true);
			} else {
				// The same URL is already being downloaded.
				return false;
			}
		}
		return true;
	}
}
*/